/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.dao;

import com.sun.istack.internal.Nullable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import team.mai.antexpress.entities.PermissionGroupEntity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static team.mai.antexpress.services.PermissionGroupService.INTERNAL_ERROR;

@SuppressWarnings("Duplicates")
@Repository
public class PermissionGroupDAO {

	@Autowired
	SessionFactory sessionFactory;

	/*
		增删改查、批量删除
	 */

	/**
	 * @param group
	 * @return
	 */
	public int insertPermissionGroup(PermissionGroupEntity group) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			Serializable save = session.save(group);
			transaction.commit();
			return (int) save;
		} catch (Exception e) {
			e.printStackTrace();
			return INTERNAL_ERROR;
		}
	}


	public boolean deletePermissionGroup(PermissionGroupEntity group) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			session.delete(group);
			transaction.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean deletePermissionGroup(PermissionGroupEntity group, Session session) {
		try {
			session.delete(group);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean deletePermissionGroup(int id) {
		try (Session session = sessionFactory.openSession()) {
			PermissionGroupEntity permissionGroupEntity = session.get(PermissionGroupEntity.class, id);
			return deletePermissionGroup(permissionGroupEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean deletePermissionGroup(int id, Session session) {
		PermissionGroupEntity permissionGroupEntity = session.get(PermissionGroupEntity.class, id);
		return deletePermissionGroup(permissionGroupEntity, session);
	}

	public boolean deletePermissionGroupsById(List<Integer> ids) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			boolean status = true;
			for (Integer id : ids) {
				if (!deletePermissionGroup(id, session)) {
					status = false;
					break;
				}
			}
			transaction.commit();
			return status;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean deletePermissionGroupsByEntity(List<PermissionGroupEntity> groups) {
		boolean status = true;
		for (PermissionGroupEntity group : groups) {
			if (!deletePermissionGroup(group)) {
				status = false;
				break;
			}
		}
		return status;
	}

	public boolean updatePermissionGroup(PermissionGroupEntity group) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			session.update(group);
			transaction.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public List<PermissionGroupEntity> getAllPermissionGroups() {
		try (Session session = sessionFactory.openSession()) {
			Query<PermissionGroupEntity> query = session.createQuery("FROM PermissionGroupEntity");
			return query.list();
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<>();
		}
	}

	@Nullable
	public PermissionGroupEntity getPermissionGroupById(int groupId) {
		try (Session session = sessionFactory.openSession()) {
			return session.get(PermissionGroupEntity.class, groupId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 检查某个组名是否存在
	 *
	 * @param groupName 组名
	 * @return {@code true} 如果组名已存在，或者出错
	 */
	public boolean ifExistsPermissionGroup(String groupName) {
		try (Session session = sessionFactory.openSession()) {
			Query query = session.createQuery("FROM PermissionGroupEntity WHERE groupName=:groupName");
			query.setParameter("groupName", groupName);
			List list = query.list();
			return !list.isEmpty();
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	}

	/**
	 * 检查某个组名是否存在，但排除本组
	 *
	 * @param groupName
	 * @return
	 */
	public boolean ifExistsPermissionGroup(String except, String groupName) {
		try (Session session = sessionFactory.openSession()) {
			Query query = session.createQuery("FROM PermissionGroupEntity WHERE groupName=:groupName");
			query.setParameter("groupName", groupName);
			List list = query.list();
			if (list.isEmpty()) {
				return false;
			} else {
				if (((PermissionGroupEntity) list.get(0)).getGroupName().equals(except)) {
					return false;
				} else {
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	}

}
